const {
  $util,
} = getApp();
Page({
  data: {
    picture: '',
    rcontent: '',
    grade: '',
    imgs: [],
    id: '',
    tid: ''
  },
  onLoad(opts) {
    this.setData(opts, () => {
      '3' !== opts.status && this.loadReport();
    });
  },
  loadReport() {
    let { id, tid } = this.data;
    let param = {};
    if (tid !== '') {
      param.id = id;
      param.tid = tid;
    } else {
      param.tid = id;
    }
    $util.request({
      url: 'entry/wxapp/partyman.investigate.report',
      method: 'GET',
      data: param,
      success: (res) => {
        let report = {
          time: res.data.time,
          picture: res.data.picture,
          rcontent: res.data.content,
          grade: res.data.grade,
        };
        res.data.picture && res.data.picture.length && (report.imgs = res.data.picture, report.lastimg = res.data.picture);
        this.setData(report);
      },
      fail: (res) => {
        console.log(res);
        wx.showModal({
          title: '提示',
          content: res.message,
          showCancel: false,
          success: () => {
            wx.navigateBack();
          },
        });
      },
    });
  },
  submit() {
    const {
      id,
      tid,
      imgs: picture,
      rcontent: content,
      grade,
    } = this.data;
    if (grade === '') {
      wx.showToast({
        title: '请填写考察评分',
        icon: 'none',
      });
      return;
    }
    const reg = /^(100|(([1-9]){1}[0-9]?|0{1})((\.)([0-9]){1,2})?)$/;
    if (!reg.test(grade)) {
      wx.showToast({
        title: '请填写正确的考察评分，评分范围0~100',
        icon: 'none',
      });
      return;
    }
    if (content === '') {
      wx.showToast({
        title: '请填写考察报告',
        icon: 'none',
      });
      return;
    }
    let param = {
      picture,
      content,
      grade,
    };
    if (tid !== '') {
      param.id = id;
      param.tid = tid;
    } else {
      param.tid = id;
    }
    $util.request({
      url: 'entry/wxapp/partyman.investigate.report',
      method: 'POST',
      data: param,
      success: (res) => {
        wx.showToast({
          title: '提交成功',
          icon: 'none',
          success: () => {
            setTimeout(() => {
              wx.navigateBack();
            }, 1000);
          },
        });
      },
    });
  },
  bindIpVal(e) {
    this.setData({
      [e.currentTarget.dataset.idx]: e.detail.value,
    });
  },
  imgsOnChange(e) {
    this.setData({
      imgs: e.detail,
    });
  },
});